#include "stdafx.h"
#include "wf_drawline.h"
#include "wf_editundo.h"



WF_DrawLine::WF_DrawLine(PF_Container& container,
                                     PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw lines",
                           container, graphicView) {

	PF_DEBUG->print("PF_ActionDrawLine::PF_ActionDrawLine");
    reset();
	PF_DEBUG->print("PF_ActionDrawLine::PF_ActionDrawLine: OK");
}



WF_DrawLine::~WF_DrawLine() {}


QAction* WF_DrawLine::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	//tr("Line: 2 Points")
    QAction* action = new QAction(tr("&2 Points"), NULL);
	action->setIcon(QIcon(":/extui/linesnormal.png"));
    //action->zetStatusTip(tr("Draw lines"));
    return action;
}



void WF_DrawLine::reset() {
	PF_DEBUG->print("PF_ActionDrawLine::reset");
    data = PF_LineAttribute(PF_Vector(false), PF_Vector(false));
    start = PF_Vector(false);
    history.clear();
	PF_DEBUG->print("PF_ActionDrawLine::reset: OK");
}



void WF_DrawLine::init(int status) {
	PF_DEBUG->print("PF_ActionDrawLine::init");
    PF_PreviewActionInterface::init(status);

    reset();
	PF_DEBUG->print("PF_ActionDrawLine::init: OK");
}



void WF_DrawLine::trigger() {
    PF_PreviewActionInterface::trigger();

    PF_Line* line = new PF_Line(container, data);
    line->setLayerToActive();
    line->setPenToActive();
    container->addEntity(line);

    // upd. undo list:
    if (document!=NULL) {
        document->startUndoCycle();
        document->addUndoable(line);
        document->endUndoCycle();
    }

	graphicView->redraw(LX_Define::RedrawDrawing);
    graphicView->moveRelativeZero(line->getEndpoint());
    PF_DEBUG->print("PF_ActionDrawLine::trigger(): line added: %d",
                    line->getId());
}



void WF_DrawLine::mouseMoveEvent(QMouseEvent* e) {
    PF_DEBUG->print("PF_ActionDrawLine::mouseMoveEvent begin");

    PF_DEBUG->print("PF_ActionDrawLine::mouseMoveEvent: snap point");
    PF_Vector mouse = snapPoint(e);
    PF_DEBUG->print("PF_ActionDrawLine::mouseMoveEvent: snap point: OK");
    if (getStatus()==SetEndpoint && data.startpoint.valid) {
    	PF_DEBUG->print("PF_ActionDrawLine::mouseMoveEvent: update preview");
        deletePreview();
        preview->addEntity(new PF_Line(preview,
                                       PF_LineAttribute(data.startpoint, mouse)));
    	PF_DEBUG->print("PF_ActionDrawLine::mouseMoveEvent: draw preview");
        drawPreview();
    }

    PF_DEBUG->print("PF_ActionDrawLine::mouseMoveEvent end");
}



void WF_DrawLine::mouseReleaseEvent(QMouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DrawLine::coordinateEvent(PF_CoordinateEvent* e) {
	PF_DEBUG->print("PF_ActionDrawLine::coordinateEvent");
    if (e==NULL) {
		PF_DEBUG->print("PF_ActionDrawLine::coordinateEvent: event was NULL");
        return;
    }

    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetStartpoint:
        data.startpoint = mouse;
        history.clear();
        history.append(mouse);
        start = data.startpoint;
        setStatus(SetEndpoint);
        graphicView->moveRelativeZero(mouse);
        updateMouseButtonHints();
        break;

    case SetEndpoint:
        data.endpoint = mouse;
        history.append(mouse);
        trigger();
        data.startpoint = data.endpoint;
        updateMouseButtonHints();
        //graphicView->moveRelativeZero(mouse);
        break;

    default:
        break;
    }
	PF_DEBUG->print("PF_ActionDrawLine::coordinateEvent: OK");
}



void WF_DrawLine::commandEvent(PF_CommandEvent* e) {
	PF_DEBUG->print("PF_ActionDrawLine::commandEvent");
    QString c = e->getCommand().toLower();

    switch (getStatus()) {
    case SetStartpoint:
        if (checkCommand("help", c)) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
            return;
        }
        break;

    case SetEndpoint:
        if (checkCommand("close", c)) {
            close();
            updateMouseButtonHints();
            return;
        }

        if (checkCommand("undo", c)) {
            undo();
            updateMouseButtonHints();
            return;
        }
        break;

    default:
        break;
    }
	PF_DEBUG->print("PF_ActionDrawLine::commandEvent: OK");
}



QStringList WF_DrawLine::getAvailableCommands() {
    QStringList cmd;

    switch (getStatus()) {
    case SetStartpoint:
        break;
    case SetEndpoint:
        if (history.size()>=2) {
            cmd += command("undo");
        }
        if (history.size()>=3) {
            cmd += command("close");
        }
        break;
    default:
        break;
    }

    return cmd;
}



void WF_DrawLine::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetStartpoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify first point"),
                                            tr("Cancel"));
        break;
    case SetEndpoint: {
            QString msg = "";

            if (history.size()>=3) {
                msg += PF_COMMANDS->command("close");
                msg += "/";
            }
            if (history.size()>=2) {
                msg += PF_COMMANDS->command("undo");
            }

            if (history.size()>=2) {
                PF_DIALOGFACTORY->updateMouseWidget(
                    tr("Specify next point or [%1]").arg(msg),
                    tr("Back"));
            } else {
                PF_DIALOGFACTORY->updateMouseWidget(
                    tr("Specify next point"),
                    tr("Back"));
            }
        }
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}


void WF_DrawLine::showOptions() {
	PF_DEBUG->print("PF_ActionDrawLine::showOptions");
    PF_ActionInterface::showOptions();

    PF_DIALOGFACTORY->requestOptions(this, true);
	PF_DEBUG->print("PF_ActionDrawLine::showOptions: OK");
}



void WF_DrawLine::hideOptions() {
    PF_ActionInterface::hideOptions();

    PF_DIALOGFACTORY->requestOptions(this, false);
}


void WF_DrawLine::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}


void WF_DrawLine::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarLines);
    }
}

void WF_DrawLine::close() {
    if (history.size()>2 && start.valid) {
        data.endpoint = start;
        trigger();
        setStatus(SetStartpoint);
        graphicView->moveRelativeZero(start);
    } else {
        PF_DIALOGFACTORY->commandMessage(
            tr("Cannot close sequence of lines: "
               "Not enough entities defined yet."));
    }
}

void WF_DrawLine::undo() {
    if (history.size()>1) {
        history.removeLast();
        deletePreview();
        graphicView->setCurrentAction(
            new WF_EditUndo(true, *container, *graphicView));
        data.startpoint = history.last();
        graphicView->moveRelativeZero(data.startpoint);
    } else {
        PF_DIALOGFACTORY->commandMessage(
            tr("Cannot undo: "
               "Not enough entities defined yet."));
    }
}

// EOF
